package Comp355Final.main;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class GUIFileDetail extends Activity{
	private Config myconfig;
	private String filepath;
    private TextView create_txtView(String name)
   {
        TextView tmp_txtview = new TextView(this);
        tmp_txtview.setText(Html.fromHtml(name));
        tmp_txtview.setTextColor(Color.BLACK);
        return tmp_txtview;
    }
    private LinearLayout create_linearlayout(TextView in_view)
    {
    	LinearLayout tmp_layout = new LinearLayout(this);
    	tmp_layout.addView(in_view);
    	return tmp_layout;
    } 
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        filepath = extras.getString("filepath");
        ScrollView scllview = new ScrollView(this);
        ScrollView.LayoutParams scllview_param = new ScrollView.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
        scllview.setBackgroundColor(Color.WHITE);
        myconfig = new Config(this);
        scllview.setBackgroundDrawable(myconfig.return_bg());
        LinearLayout Llay = new LinearLayout(this);
        LinearLayout.LayoutParams Llay_param = new LinearLayout.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
        LinearLayout.LayoutParams tmp_layout_param = new LinearLayout.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.WRAP_CONTENT);
        Llay.setOrientation(LinearLayout.VERTICAL);
        //Utility.quick_dialog(filepath, this);
        File myfile = new File(filepath);
        String filename=myfile.getName();


     
        
        String file_extension = new String();
        if(myfile.isDirectory())
        {
        //	Llay.addView(create_linearlayout(create_txtView("<B><u>Folder Name: </u></B><br>"+filename+"<br>")),tmp_layout_param);
        	Llay.addView(create_linearlayout(create_txtView("<B><u>Folder name:</u></B><br>"+filename+"<br>")),tmp_layout_param);
        	Llay.addView(create_linearlayout(create_txtView("<B><u>Folder path: </u></B><br>"+filepath+"<br>")),tmp_layout_param);
        	FileIO fileio = new FileIO(filename);
        	ArrayList<String> file_list = fileio.scan_folder();
        	Llay.addView(create_linearlayout(create_txtView("<B><u>Files in the folder: </u></B><br>"+file_list.size()+" Files"+"<br>")),tmp_layout_param);
        	long sizeOfFolder=0;
        	for(int i=0;i<file_list.size();i++)
        	{
        		sizeOfFolder=sizeOfFolder+FileIO.fileSize(file_list.get(i));
        	}
        	Llay.addView(create_linearlayout(create_txtView("<B><u>Folder Size(Include files inside): </u></B><br>"+sizeOfFolder/1024+" KB"+"<br>")),tmp_layout_param);
        }
        else if(myfile.isFile())
        {
        	Llay.addView(create_linearlayout(create_txtView("<B><u>File Name: </u></B><br>"+filename+"<br>")),tmp_layout_param);
        	
        	 String tmp_split[]=filename.split("\\.");
        	 file_extension = tmp_split[tmp_split.length-1];
        	 Llay.addView(create_linearlayout(create_txtView("<B><u>File Type: </u></B><br>"+file_extension+"<br>")),tmp_layout_param);
        	 Llay.addView(create_linearlayout(create_txtView("<B><u>File Path: </u></B><br>"+filepath+"<br>")),tmp_layout_param);
        	 Llay.addView(create_linearlayout(create_txtView("<B><u>File size: </u></B><br>"+myfile.length()/1024+" KB"+"<br>")),tmp_layout_param);

        }
   	 Calendar mycal = Calendar.getInstance();
	 mycal.setTimeInMillis(myfile.lastModified());
	 Llay.addView(create_linearlayout(create_txtView("<B><u>Last modified time: </u></B><br>"+mycal.get(Calendar.HOUR_OF_DAY)+":"+mycal.get(Calendar.MINUTE)+" "+mycal.get(Calendar.DAY_OF_MONTH)+"/"+mycal.get(Calendar.MONTH)+"/"+mycal.get(Calendar.YEAR)+"<br>")),tmp_layout_param);
       
       
        
        scllview.addView(Llay, Llay_param);
        
        this.setTitle("File Details - "+filename);
        this.setContentView(scllview, scllview_param);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0, 0, 0, "Return to BookShelf").setIcon(android.R.drawable.ic_menu_revert);
    	return super.onCreateOptionsMenu(menu); 
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	if(item.getItemId()==0)
    	{
    		Intent int_ent = new Intent(this, GUIBookShelfBkSelect.class);
    		this.startActivity(int_ent);
    		finish();
    	}
    	return super.onOptionsItemSelected(item); 
    }
}
